package com.mh.www.mh_frame.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.mh.www.mh_frame.R;
import com.mh.www.mh_frame.view.activity.base.BaseActivity;
import com.mh.www.mh_frame.view.fragment.DailyInquiryFragment;
import com.mh.www.mh_frame.view.fragment.FieldInspectionFragment;
import com.mh.www.mh_frame.view.fragment.MyDailyFragment;
import com.mh.www.mh_frame.view.fragment.ProgressManageFragment;
import com.mh.www.mh_frame.view.fragment.QueryEntryFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = "MainActivity";
    List<Fragment> mFragments = new ArrayList<>();
    @BindView(R.id.rb_query_entry)
    RadioButton mRbQueryEntry;
    @BindView(R.id.rb_daily_inquiry)
    RadioButton mRbDailyInquiry;
    @BindView(R.id.rb_my_daily)
    RadioButton mRbMyDaily;
    @BindView(R.id.rb_progress_manage)
    RadioButton mRbProgressManage;
    @BindView(R.id.rb_field_inspection)
    RadioButton mRbFieldInspection;
    @BindView(R.id.rg_main)
    RadioGroup mRgBot;
    @BindView(R.id.home_framelayout)
    FrameLayout mHomeFramelayout;
    @BindView(R.id.container)
    RelativeLayout mContainer;

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_main2);

    }

    @Override
    public void initView() {

        mFragments.add(new QueryEntryFragment());
        mFragments.add(new DailyInquiryFragment());
        mFragments.add(new MyDailyFragment());
        mFragments.add(new ProgressManageFragment());
        mFragments.add(new FieldInspectionFragment());
        mRgBot.setOnCheckedChangeListener(this);
        mRbMyDaily.setChecked(true);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }

    @OnClick({R.id.rb_query_entry, R.id.rb_daily_inquiry, R.id.rb_my_daily, R.id.rb_progress_manage, R.id.rb_field_inspection})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_query_entry:
                break;
            case R.id.rb_daily_inquiry:
                break;
            case R.id.rb_my_daily:
                break;
            case R.id.rb_progress_manage:
                break;
            case R.id.rb_field_inspection:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_query_entry:
                selectFragment(0);
                break;
            case R.id.rb_daily_inquiry:
                selectFragment(1);
                break;
            case R.id.rb_my_daily:
                selectFragment(2);
                break;
            case R.id.rb_progress_manage:
                selectFragment(3);
                break;
            case R.id.rb_field_inspection:
                selectFragment(4);
                break;
        }
    }

    int mFragmentIndex = -1;

    private void selectFragment(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (mFragmentIndex != -1) ft.hide(mFragments.get(mFragmentIndex));
        if (fm.getFragments() != null && fm.getFragments().contains(mFragments.get(position))) {
            ft.show(mFragments.get(position));
        } else {
            ft.add(R.id.home_framelayout, mFragments.get(position));
        }
        mFragmentIndex = position;
        ft.commit();
    }
}
